const gameLogic = require('./gameLogic');
const utils = require('../../util/utils');
const gameProto = require('./gameProto');
const roomProto = require('../roomFrameProto');
const logger = require('pomelo-logger').getLogger('sss');
const moment = require('moment');
const roomBaseFrame = require('../roomBaseFrame');
const enumeration = require('../../constant/enumeration');

class gameFrame extends roomBaseFrame {


	constructor(roomID, creatorInfo, gameRule) {

		super(roomID, creatorInfo, gameRule);

		this.handCards = {}; //玩家手牌
		this.baoCards = {}; //玩家宝牌
		this.cloneHandCards = {}; //总手牌
		this.gameStatus = gameProto.gameStatus.NONE; //等待状态
		this.result = null; //游戏结果
		this.bankerUid = null;
		this.winList = {}; //玩家所有输赢
		this.robBankList = []; //抢庄玩家
		this.userBetScore = {}; //下分玩家
		this.showCardsUser = [];
		this.userTestCards = {}; //玩家测试卡牌
		this.userWashList = []; //切牌玩家
		this.teShuCard = {}; //玩家特殊牌

		this.baoCardsBegin = 100;

		//操作玩家
		this.totalTick = 0; //当前阶段总时间
		this.tick = 0; //倒计时时间
		this.operateTimer = null;
		this.endOperateTime = null; //倒计时定时器
		this.userRobScoreList = [0, 1, 2, 3, 4, 5];
		this.userBetScoreList = [1, 2, 3, 4, 5];

		//gameRule.baoCards = true;

		//十三水新增
		this.wanfa = this.gameRule.wanfa || 2; // 游戏类型 1:抢庄 2：通比 3: 看牌下注
		this.jokerCount = this.gameRule.jokerCount || 0; //鬼牌数量
		this.maPaiType = this.gameRule.maPaiType || 0; //0 无马牌 1: 随机马牌 2：固定马牌
		this.mapai = this.gameRule.mapai || []; //固定马牌值
		this.maCount = this.gameRule.maCount || 1; //马牌值
		//this.maRateType = this.gameRule.maRateType || 1; //1 单马 2：多马
		this.maRateType = 2; //1 单马 2：多马
		this.colors = this.gameRule.colors; //加的花色
		this.shouDongBaiPai = this.gameRule.shouDongBaiPai || false; //只允许手动摆牌
		this.tesupai = this.gameRule.tesupai || false; //特殊牌型
		this.tongHuaBiDui = this.gameRule.tongHuaBiDui || false; //同花比对
		this.jifen = this.gameRule.jifen || 1; //打枪的分数 1:加倍 2：加一分
		this.biPaiType = this.gameRule.biPaiType || 1; // 比牌方式  1：通比 2：每道比牌 3：跳过比牌
		this.liPaiTime = this.gameRule.liPaiTime || 0; // 0 无限制  其他为可理牌时间
		this.gameBeginTime = null; //可以解散时间
		this.startBeginTime = null; //默认3秒后准备
		this.gameDelayTime = this.gameRule.gameDelayTime || 5;
		this.lianFaType = this.gameRule.lianFaType || 1; //1234
		this.hasChongSan = this.gameRule.hasChongSan || false; //是否有冲三玩法

		//百变十三水才有包牌
		gameRule.baoCards = (this.gameType == enumeration.gameType.BBSSS);

		this.gameLogic = new gameLogic(gameRule);

		//this.liPaiTime = 3;
		//统计详情
		this.daQiangCount = {};
		this.beiDaQiangCount = {};
		this.tongHuaShunCount = {};
		this.teShuCount = {};
		this.quanLeiDaCount = {};

		//输赢统计
		this.winCount = {};
		this.loseCount = {};
		this.tieCount = {};

		this.washGold = 5;

		this.robotReadyTimer = null;

	}

	/*----------------------------------------------------消息交互--------------------------------------------------------*/
	//游戏获取玩家发送的消息
	receiveGameMessage(uid, msg) {

		let type = msg.type || null;
		let data = msg.data || null;
		if (!type || !data) return;

		if (type === gameProto.GAME_REVIEW_NOTIFY) {
			//牌局回放
			this.onGameReview(uid);
		} else if (type === gameProto.GAME_ROB_BANK_NOTIFY) {
			//抢庄
			this.onRobBank(uid, data);
		} else if (type === gameProto.GAME_USER_SCORE_NOTIFY) {
			//下分
			this.onPourScore(uid, data)
		} else if (type === gameProto.SHOW_CARDS_NOTIFY) {
			//亮牌
			this.onShowCards(uid, data);
		} else if (type === gameProto.GAME_CHANGE_BAO_CARD_NOTIFY) {
			//切换宝
			this.onChangeBaoCard(uid, data);
		} else if (type == gameProto.GAME_SET_TEST_CARDS_NOTIFY) {
			//玩家换牌
			this.onChangeCard(uid, data.cardData);

		} else if (type == gameProto.GAME_GET_TEST_CARDS_NOTIFY) {
			//玩家查询牌库
			this.onSearchCardsList(uid);
		} else if (type == gameProto.GAME_USER_WASH_CARD_NOTIFY) {
			//洗牌
			this.onUserWashCard(uid);
		} else if (type === gameProto.GAME_USER_CHOOSE_WASH_NOTIFY) {
			//结束选择洗牌
			this.userChooserWash(uid);
		} else if (type === gameProto.GAME_START_TIME_DELAY_NOTIFY) {
			//动画时间
			this.onDelayTime(data);
		}
	};

	/*----------------------------------------------------游戏初始化--------------------------------------------------------*/

	//重置游戏状态
	resetGame() {

		this.clearOperateTimer();
		//重置游戏状态
		this.gameStatus = gameProto.gameStatus.NONE;
		this.sendGameStatus(this.gameStatus);

		let userList = this.getPlayingUserList();
		this.result = null;
		this.bankerUid = null;
		this.robBankList = [];
		this.userBetScore = {};

		this.operateTimer = null;
		this.endOperateTime = null; //倒计时定时器
		this.showCardsUser = []; //普通摆牌玩家
		this.startBeginTime = null;
		//每次玩家投注信息
		for (let i = 0; i < userList.length; i++) {
			let uid = userList[i];
			this.handCards[uid] = []; //玩家当前手牌手牌
			this.baoCards[uid] = []; //玩家宝牌
			this.showCardsUser.push({ uid, isShow: false });
			this.cloneHandCards[uid] = []; //玩家所有的手牌
		}
	};

	getEnterGameData(uid) {

		let curTick = (this.endOperateTime || moment()).diff(moment(), 's', true);
		let sendData = {
			washGold: this.washGold,
			gameStatus: this.gameStatus,
			gameStarted: this.gameStarted,
			handCards: this.getHandCardsByUid(uid),
			baoCards: this.baoCards[uid],
			showCardsUser: this.showCardsUser,
			teShuType: this.teShuCard[uid],
			bankerUid: this.bankerUid,
			curBureau: this.curBureau,
			tick: curTick < 0 ? 0 : curTick,
			userWinTotalScore: this.userWinTotalScore,
			gameRule: this.gameRule,
			totalTick: this.totalTick,
			maxBureau: this.maxBureau,
			chairCount: this.chairCount,
			userWashList: this.userWashList,
			userPlayStatus: this.getAllUserPlayStatus(),
			result: this.result,
			userList: this.getPlayingUserList(),
			userBetScore: this.userBetScore,
			robBankList: this.robBankList,
			mapai: this.gameLogic.getMaPai()
		};
		return sendData;
	};

	//延迟开始时间
	onDelayTime(data) {

		let times = parseInt(data.times) + 5;
		this.startBeginTime = moment().add(times, 's');
		this.gameDelayTime = times;

	}

	/*----------------------------------------------------游戏开始--------------------------------------------------------*/

	//游戏开始
	onEventGameStart() {

		if (this.robotReadyTimer) {
			clearInterval(this.robotReadyTimer);
			this.robotReadyTimer = null;
		}

		if (this.gameStatus != gameProto.gameStatus.NONE) return;
		this.resetGame();
		//局数推送
		this.curBureau += 1;
		this.sendGameData(gameProto.gameBureauPush(this.curBureau));

		this.gameStarted = true;
		this.showLog(`[ssssss] game start ...`);

		this.startWashCards();

	};

	/*----------------------------洗牌----------------------------------*/

	startWashCards() {

		if (this.userWashList.length > 0) {
			this.showLog(`[ssssss] game wash cards ...`);
			this.gameStatus = gameProto.gameStatus.WASH_CARD; /* 游戏状态 */
			this.totalTick = gameProto.gameStatusTm.WASH_CARD;
			this.sendGameStatus(this.gameStatus, this.totalTick);
			this.sendGameData(gameProto.gameUserWashCardPush(this.userWashList, this.totalTick));

			this.operateTimer = setTimeout(() => {
				//开始游戏
				if (this.gameStatus == gameProto.gameStatus.WASH_CARD) {
					this.startBeginGame();
				}
			}, this.totalTick * 1000);
		} else {
			//如果没有则直接开始发牌
			this.startBeginGame();
		}
	}

	//用户洗牌
	onUserWashCard(uid) {

		let index = this.userWashList.findIndex(item => item == uid);
		this.userWashList.splice(index, 1);
		if (this.userWashList.length == 0 && this.gameStatus == gameProto.gameStatus.WASH_CARD) {
			this.startBeginGame();
		}
	}

	//用户选择切牌
	async userChooserWash(uid) {

		if (this.gameStatus != gameProto.gameStatus.NONE) return;
		if (this.userWashList.length > 0) {
			let washUid = this.userWashList[0];
			this.sendGameData(gameProto.userChooseWashResult(uid, this.userWashList, false, '玩家' + washUid + '已经洗牌'), [uid]);
		} else {
			//判断钻石是否足够
			let baseGold = 0;
			if (this.gameRule.payType == enumeration.roomPayType.AAZHIFU) {
				baseGold = this.maxBureau - this.curBureau;
			} else if (this.gameRule.payType == enumeration.roomPayType.YINGJIAZHIFU) {
				baseGold = this.getRoomPayDiamond();
			}
			let curUser = await this.userPayWash(uid, baseGold, this.washGold);
			if (curUser) {

				this.userWashList.push(uid);
				this.sendGameData(gameProto.userChooseWashResult(uid, this.userWashList, true, '洗牌成功'));
				this.userReady(uid);

			} else {
				this.sendGameData(gameProto.userChooseWashResult(uid, this.userWashList, false, '钻石不足'), [uid]);
			}
		}
	}

	//玩家请求换宝牌
	onChangeBaoCard(uid, data) {

		this.showLog(`[${uid}] user change bao card data = ${JSON.stringify(data)} userBaoCard = ${JSON.stringify(this.baoCards[uid])} `);

		let card = data.baoCard;
		let isEnd = data.isEnd;

		if (!isEnd) {
			let curIndex = this.baoCards[uid].findIndex(item => item.baoCard == card);
			if (curIndex != -1) {
				let curDetail = this.baoCards[uid][curIndex];
				if (curDetail.count < 3) {
					let changeCard = this.gameLogic.getChangeCard();
					curDetail.changeCard = changeCard;
					curDetail.count += 1;
					//推送给玩家
					this.showLog(`[${uid}] user change bao card = ${card} changeCard = ${changeCard} count = ${curDetail.count}`);
				}
			}
		}
		isEnd = isEnd ? isEnd : this.baoCards[uid].every(item => item.count >= 3);
		if (isEnd) {
			let baoList = this.baoCards[uid];
			for (let i = 0; i < baoList.length; i++) {
				let curDetail = this.baoCards[uid][i];
				if (!curDetail.isEnd) {
					let changeCard = curDetail.changeCard == -1 ? this.gameLogic.getChangeCard() : curDetail.changeCard;
					curDetail.endCard = changeCard;
					curDetail.count = 3;
					this.showLog(`[${uid}] user end bao card card = ${curDetail.baoCard}  changeCard = ${changeCard}`);
					curDetail.isEnd = true;
					this.handCards[uid].push(changeCard);
				}
			}
			this.handCards[uid] = this.gameLogic.sortBaseCard(this.handCards[uid]);
		}
		//20240922修改成换一次重新发手牌和宝牌
		let curCards = this.getHandCardsByUid(uid);
		this.sendGameData(gameProto.gameChangeBaoCardPush(uid, curCards, this.baoCards[uid], card), [uid]);
	}

	//20240922弃用 适合单刷宝牌-备用
	checkBaoEnd(uid) {

		let unChangeCard = this.baoCards[uid].find(item => item.endCard == -1);
		if (unChangeCard) {
			//推送给玩家
			this.sendGameData(gameProto.gameChangeBaoCardPush(unChangeCard.changeCard, unChangeCard.count), [uid]);
		} else {

			//换牌完毕 直接发最新的牌
			let curCards = this.getHandCardsByUid(uid);
			//判断是否特殊牌型
			let teShuType = 0;
			if (this.tesupai) {
				let curUserCards = this.handCards[uid];
				if (curUserCards.length == 13) {
					teShuType = this.gameLogic.getTeShuPaiCardType(curUserCards.slice()) || 0;
					this.teShuCard[uid] = teShuType;
				}
			}
			let curList = this.getPlayingUserList();
			this.sendGameData(gameProto.gameSendCardPush(curCards, (this.gameLogic.getMaPai() || -1), teShuType, curList, this.baoCards[uid]), [uid]);
		}
	}

	/*----------------------------------------------------发牌--------------------------------------------------------*/
	//开始游戏动画
	startBeginGame() {

		this.clearOperateTimer();

		this.gameStatus = gameProto.gameStatus.START_GAME;
		this.totalTick = gameProto.gameStatusTm.START_GAME;
		this.sendGameStatus(this.gameStatus, this.totalTick);

		//推送发牌消息
		setTimeout(() => {
			this.startSendCards();
		}, this.totalTick * 1000)

	}

	//获取所有配牌
	getControlCards(allCards) {

		let curCards = allCards.slice();
		let userList = this.getPlayingUserList();
		for (let i = 0; i < userList.length; i++) {
			let uid = userList[i];
			let curUser = this.getUserByUID(uid);
			let userType = curUser.userInfo.userType;
			if (userType == 1) {
				//黑名单 
				let cardList = this.gameLogic.getHeiCards(curCards);
				this.showLog(`[${uid}] hei user get cards = ${cardList}`, false);
				if (cardList.length > 0) {
					this.userTestCards[uid] = cardList.slice();
					curCards = this.gameLogic.getExceptCards(this.userTestCards, curCards);
				}
			} else if (userType >= 2) {
				//白名单
				let cardList = this.gameLogic.getBaiCards(curCards, userType);
				this.showLog(`[${uid}] bai user get cards = ${cardList}`, false);
				if (cardList) {
					this.userTestCards[uid] = cardList.slice();
					curCards = this.gameLogic.getExceptCards(this.userTestCards, curCards);
				}
			}
		}
	}

	//发牌流程
	startSendCards() {

		this.userWashList = []; //重置切牌
		this.clearOperateTimer();

		//获取扑克牌 
		let allCards = this.gameLogic.initAllCards();
		allCards = this.gameLogic.getExceptCards(this.userTestCards, allCards);

		let maPaiInfo = this.gameLogic.getMaPai();
		this.showLog(`[ssssss] game start send cards  maPaiInfo = ${maPaiInfo} count = ${allCards.length}`);

		//给黑白名单配牌
		this.getControlCards(allCards);
		let curAllCards = this.gameLogic.initAllCards();
		allCards = this.gameLogic.getExceptCards(this.userTestCards, curAllCards);
		this.showLog(`[ssssss] game start send cards  count = ${allCards.length}`);


		//this.userTestCards['385878'] = [15, 28, 28, 34, 34, 21, 21, 8, 10, 10, 23, 23, 49]

		//给其他玩家发牌
		allCards = this.gameLogic.washCards(allCards);



		let baseTestCards = [
			[[32, 53, 52, 3, 16, 29, 42, 37, 4, 17, 30, 43, 8], [19, 53, 52, 2, 15, 28, 41, 36, 5, 18, 31, 44, 33]],
			[[8, 21, 53, 1, 14, 27, 40, 29, 7, 20, 33, 46, 30], [2, 53, 53, 5, 18, 44, 31, 32, 10, 23, 36, 36, 49]],
			[[32, 52, 53, 3, 16, 29, 42, 37, 4, 17, 30, 43, 21], [19, 52, 53, 2, 15, 28, 41, 23, 5, 18, 31, 44, 33]],
			[[8, 21, 34, 3, 16, 29, 42, 14, 6, 19, 32, 45, 32], [8, 21, 53, 30, 30, 43, 15, 4, 7, 20, 33, 46, 33]],
			[[5, 53, 53, 6, 20, 34, 22, 10, 29, 42, 16, 15, 41], [7, 20, 53, 8, 21, 34, 32, 50, 4, 17, 30, 1, 14]],
			[[5, 53, 53, 14, 17, 19, 21, 22, 7, 20, 33, 46, 36], [5, 52, 53, 6, 19, 32, 46, 34, 4, 17, 30, 43, 16]],
			[[5, 53, 53, 6, 20, 34, 22, 10, 25, 38, 51, 15, 28], [5, 52, 52, 8, 21, 34, 1, 15, 45, 32, 19, 40, 27]],
			[[6, 53, 53, 2, 15, 28, 1, 14, 4, 17, 30, 43, 30], [52, 52, 53, 3, 17, 18, 6, 7, 8, 21, 34, 47, 34]],
			[[53, 53, 52, 6, 19, 32, 45, 33, 4, 17, 30, 43, 30], [53, 52, 52, 5, 18, 31, 44, 21, 3, 16, 29, 42, 29]],
			[[53, 53, 53, 18, 31, 44, 5, 17, 8, 21, 34, 34, 47], [53, 53, 53, 6, 19, 32, 33, 45, 9, 22, 48, 35, 35]],
			[[53, 53, 52, 2, 3, 0, 13, 26, 7, 20, 33, 20, 33], [53, 53, 52, 52, 52, 53, 41, 29, 32, 28, 28, 21, 29]]
		]

		let curCardsTest = baseTestCards[this.curBureau - 1];
		if (curCardsTest) {
			this.userTestCards['385878'] = curCardsTest[0];
			this.userTestCards['759403'] = curCardsTest[1];
		}



		let userList = this.getPlayingUserList();
		for (let i = 0; i < userList.length; i++) {
			let uid = userList[i];
			if (this.userTestCards[uid]) {
				this.showLog(`[${uid}] get test cards = ${this.gameLogic.sortBaseCard(this.userTestCards[uid])} `);
				this.cloneHandCards[uid] = this.userTestCards[uid];
				let leftCount = 13 - (this.cloneHandCards[uid]).length;
				if (leftCount > 0) {
					let cards = allCards.splice(0, leftCount);
					this.cloneHandCards[uid] = this.cloneHandCards[uid].concat(cards);
				}
				delete this.userTestCards[uid];
			} else {
				let cards = allCards.splice(0, 13);
				this.showLog(`[${uid}] get cards = ${this.gameLogic.sortBaseCard(cards)} `);
				this.cloneHandCards[uid] = cards.slice();
			}
		}

		//马牌消息推送
		let mapai = this.gameLogic.getMaPai();
		this.sendGameData(gameProto.gameMaPaiPush(mapai));

		if (this.wanfa == gameProto.gameType.TONGBI) {
			//通比直接发13张牌
			this.startSendCountCards(13);
		} else {
			//其余模式发8张牌
			this.startSendCountCards(8);
		}

	};

	//发指定张数扑克
	startSendCountCards(count) {

		this.gameStatus = gameProto.gameStatus.SEND_CARDS;
		this.totalTick = gameProto.gameStatusTm.SEND_CARDS;
		this.endOperateTime = moment().add(this.totalTick, 's');
		this.sendGameStatus(this.gameStatus, this.totalTick);
		//发牌
		for (let uid in this.cloneHandCards) {
			let curCards = this.cloneHandCards[uid].splice(0, count);
			let curList = curCards.filter(item => item < this.baoCardsBegin);
			let baoList = curCards.filter(item => item >= this.baoCardsBegin);
			for (let i = 0; i < baoList.length; i++) {
				let baoCard = baoList[i];
				let details = { baoCard, count: 0, changeCard: -1, endCard: -1 };
				this.baoCards[uid].push(details);
			}
			curList = this.handCards[uid].concat(curList);
			this.handCards[uid] = this.gameLogic.sortBaseCard(curList);
			//this.showLog(`[${uid}] send cards = ${this.handCards[uid]} baoList = ${JSON.stringify(baoList)} `);
		}
		//推送消息
		for (let uid in this.handCards) {
			let curCards = this.getHandCardsByUid(uid);
			//判断是否特殊牌型
			let teShuType = 0;
			if (this.tesupai) {
				let curUserCards = this.handCards[uid];
				if (curUserCards.length == 13) {
					teShuType = this.gameLogic.getTeShuPaiCardType(curUserCards.slice()) || 0;
					this.teShuCard[uid] = teShuType;
				}
			}
			let curList = this.getPlayingUserList();
			let curMaPai = (this.gameLogic.getMaPai() || -1);
			this.sendGameData(gameProto.gameSendCardPush(curCards, curMaPai, teShuType, curList, this.baoCards[uid]), [uid]);
		}

		//直接通知玩家理牌
		setTimeout(() => {
			this.startShowCards();
		}, this.totalTick * 1000)

	}

	//根据uid或者可以看到的手牌
	getHandCardsByUid(uid) {

		let curCloneCards = {};
		let curUser = this.getUserByUID(uid);
		if (curUser && curUser.userInfo.isTest) {
			//调试权限可以看所有牌
			return this.handCards;
		} else {
			for (let key in this.handCards) {
				let curCards = this.handCards[key].slice();
				if (key == uid) {
					curCloneCards[key] = curCards;
				} else {
					curCloneCards[key] = curCards.fill(60);
				}
			}
			return curCloneCards;
		}
	}

	/*----------------------------------------------------亮牌--------------------------------------------------------*/

	//给玩家推送亮牌操作
	startShowCards() {

		//先变更阶段
		this.gameStatus = gameProto.gameStatus.SHOW_CARD;
		this.totalTick = this.liPaiTime;
		if (this.totalTick > 0) {
			this.endOperateTime = moment().add(this.totalTick, 's');
			this.operateTimer = setTimeout(() => {
				//自动理牌
				if (this.gameStatus == gameProto.gameStatus.SHOW_CARD) {
					let userList = this.getPlayingUserList();
					for (let i = 0; i < userList.length; ++i) {
						let uid = userList[i];
						if (this.showCardsUser.find(item => item.uid == uid && !item.isShow)) {
							this.userAutoOperate(uid);
						}
					}
				}
			}, this.totalTick * 1000);
		}
		this.robotAuto();
		this.showLog(`[ssssss] game start show cards`, false);
		this.sendGameStatus(this.gameStatus, this.totalTick);
	}

	//玩家亮牌
	async onShowCards(uid, data) {

		this.showLog(`[${uid}] user show card data = ${JSON.stringify(data)} `);
		if (this.gameStatus != gameProto.gameStatus.SHOW_CARD) {
			this.showLog(`[${uid}] game show cards status error`, true);
			return;
		}

		//是否可以重复摆牌
		// if (this.showCardsUser.find(item => item.uid == uid && item.isShow)) {
		// 	this.showLog(`[${ uid }]game already show cards`, true);
		// 	return;
		// }

		//玩家手牌
		let cards = this.handCards[uid];
		//0 普通牌型 1 特殊牌型
		let type = data.type || 0;
		//特殊牌型
		if (type) {

			//特殊牌型
			if (this.tesupai && cards.length == 13) {
				let teShuType = this.gameLogic.getTeShuPaiCardType(cards);
				let teShuScore = this.gameLogic.getTeShuScore(teShuType);
				this.showLog(`[${uid}] teShuType = ${teShuType} teShuScore = ${teShuScore}`);
				if (teShuScore > 0) {
					//特殊牌加一
					this.teShuCount[uid] = (this.teShuCount[uid] || 0) + 1;
					let sortCards = this.gameLogic.sortSpecialCard(cards);
					let curShowCards = { uid, teShuScore, teShuType, type: type, cards: sortCards, isShow: true };
					let info = this.showCardsUser.find(info => info.uid == uid);
					Object.assign(info, curShowCards)
					this.sendGameData(gameProto.showCardsPush(curShowCards, uid));
				} else {
					this.showLog(`[${uid}] cards is not teshupai = ${JSON.stringify(cards)} `, true);
					return;
				}
			} else {
				this.showLog(`[${uid}] game have no tesupai`, true);
				return;
			}
		} else {

			//普通牌型
			let curCards = data.cards.slice();
			//this.showLog(`[${uid}] show cards = ${JSON.stringify(curCards)} `);

			//判断大小是否正确 是否在手牌中
			if (this.gameLogic.getCardsType(curCards) && this.isInCardList(curCards, this.handCards[uid])) {

				let sortCards = this.gameLogic.sortBaiCardList(curCards);
				let curShowCards = { uid, teShuScore: 0, teShuType: 0, type: type, cards: sortCards, isShow: true };

				//记录同花顺牌型次数
				let zhongCard = this.gameLogic.getZhongArr(sortCards);
				let weiCard = this.gameLogic.getWeiArr(sortCards);
				let zhongType = this.gameLogic.getCardType(zhongCard);
				let weiType = this.gameLogic.getCardType(weiCard);
				let tonghuaCount = 0
				if (zhongType == 'tonghuashun') {
					tonghuaCount += 1;
				}
				if (weiType == 'tonghuashun') {
					tonghuaCount += 1;
				}
				this.tongHuaShunCount[uid] = (this.tongHuaShunCount[uid] || 0) + tonghuaCount;

				//推送摆牌消息
				let info = this.showCardsUser.find(info => info.uid == uid);
				Object.assign(info, curShowCards)
				this.sendGameData(gameProto.showCardsPush(curShowCards, uid));

			} else {
				this.showLog(`[${uid}] wrongCards = ${JSON.stringify(curCards)} userCards = ${JSON.stringify(this.handCards[uid])} `, true);
				return;
			}
		}
		let playUser = this.getPlayingUserList();
		let showUser = this.showCardsUser.filter(item => item.isShow);
		this.showLog(`[${uid}] user show card  playUser = ${playUser.length} showUser = ${showUser.length} `);

		if (playUser.length <= showUser.length) {
			this.endShowCards();
		}
	}

	//结束亮牌
	endShowCards() {

		this.showLog(`[ssssss] game end show cards`);
		this.clearOperateTimer();
		//开始结算
		this.startResult();
		//this.startBeginTime = moment().add(5, 's');
	}

	/*----------------------------------------------------计算结果--------------------------------------------------------*/

	//开始计算结果
	async startResult() {

		this.clearOperateTimer();
		if (this.roomDismissed) return;
		//摆牌结束 玩家结算
		this.gameStatus = gameProto.gameStatus.RESULT;
		this.totalTick = gameProto.gameStatusTm.RESULT;
		//延迟结算
		// await this.delay(1500);
		this.showLog(`[ssssss] game start count result`);
		this.sendGameStatus(this.gameStatus, this.totalTick);
		//计算分数并保存
		await this.addResultRecord();
		//当前游戏结束
		await this.gameEnd();

	};

	//计算分数并保存结果
	async addResultRecord() {

		let mianbaiArr = {};
		let cardArr = {};
		let playingUser = [];
		let userList = {};

		//统计特殊牌型及普通牌型
		for (let i = 0; i < this.showCardsUser.length; i++) {
			let curCards = this.showCardsUser[i];
			if (curCards.type) {
				mianbaiArr[curCards.uid] = curCards.type;
			}
			cardArr[curCards.uid] = curCards.cards;
			playingUser.push(curCards.uid);
			let user = this.getUserByUID(curCards.uid);
			if (user && user.userInfo) {
				userList[curCards.uid] = { uid: user.userInfo.uid, nickname: user.userInfo.nickname, avatar: user.userInfo.avatar }
			}
		}

		let result = this.gameLogic.getResout(cardArr, mianbaiArr, this.baseScore, playingUser, this.bankerUid);

		//this.showLog('[fjsss] gameResult  ' + JSON.stringify(result));

		let { endScore, scoreArr } = this.getScoreArrByResout(result, this.bankerUid);
		let curScores = scoreArr;
		//this.showLog('[fjsss] gameResult  ' + JSON.stringify(scoreArr));
		//this.showLog('[fjsss] gameResult  ' + JSON.stringify(endScore));
		result.daqiangSort = this.getDaQiangSort(result.daqiangArr);
		// /let curScores = this.reCountUserScore(JSON.parse(JSON.stringify(scoreArr)));
		result.endScore = endScore;
		result.totalScore = curScores;
		// this.teShuSort.sort((a, b) => { return a.score - b.score });
		// result.teShuSort = this.teShuSort;
		result.userList = userList;
		result.mapai = this.gameLogic.getMaPai();
		result.userBetScore = this.userBetScore;
		result.robBankList = this.robBankList;
		result.gameRule = this.gameRule;
		result.roomID = this.roomID;
		result.endTime = Date.now();

		this.result = result;
		//回放记录
		let message = gameProto.gameResultPush(result, this.bankerUid);
		this.sendGameData(message);
		this.reviewRecord.push(message.data);


		//统计分数
		let endData = [];
		for (let key in curScores) {
			//本局输赢
			endData.push({ uid: key, score: curScores[key] });
			//统计数据
			this.userWinTotalScore[key] = (this.userWinTotalScore[key] || 0) + curScores[key];

			if (curScores[key] > 0) {
				this.winCount[key] = (this.winCount[key] || 0) + 1;
			} else if (curScores[key] < 0) {
				this.loseCount[key] = (this.loseCount[key] || 0) + 1;
			} else {
				this.tieCount[key] = (this.tieCount[key] || 0) + 1;
			}

			//数据统计值
			let user = this.getUserByUID(key);
			if (user) {
				if (!this.userWinRecord[user.userInfo.uid]) {
					this.userWinRecord[user.userInfo.uid] = {
						uid: user.userInfo.uid,
						nickname: user.userInfo.nickname,
						avatar: user.userInfo.avatar,
						score: 0,
						winScore: 0,
					};
				}
				this.userWinRecord[user.userInfo.uid].winScore += curScores[key];
				this.userWinRecord[user.userInfo.uid].score += curScores[key];
			}
		}
		this.recordGameResult(endData).catch((err) => { logger.error(err.stack); });
	}



	//按照顺序打枪
	getDaQiangSort(daqiangArr) {


		let daqiangSort = [];
		for (let i = 0; i < this.chairCount; i++) {

			let curUser = this.getUserByChairID(i);
			if (curUser) {
				let uid = curUser.userInfo.uid;
				let curDetail = daqiangArr[uid];
				let daUser = [];
				for (let key in curDetail) {
					if (curDetail[key] < 0) {
						daUser.push(key);
					}
				}
				if (daUser.length > 0) {
					daqiangSort.push({ uid, daUser });
				}
			}
		}
		return daqiangSort;
	}


	//v2.0获取最后算分
	getScoreArrByResout(resout, bankerUid) {

		let i, j, k;
		//游戏的分数结果
		if (!resout) {
			resout = this.result;
		}

		let playingChairArr = this.getPlayingUserList();
		let memberCount = playingChairArr.length;

		let scoreArr = {};
		let endScore = {};
		for (i = 0; i < memberCount; ++i) {
			scoreArr[playingChairArr[i]] = 0;
			endScore[playingChairArr[i]] = [0, 0, 0];
		}

		let daqiang, guaiScore, totalRate;

		let isCanQuanLeiDa = true;
		for (let key in resout.daqiangArr) {

			for (let myKey in resout.daqiangArr[key]) {
				let curDaQiang = resout.daqiangArr[key][myKey];
				if (curDaQiang) {
					if (curDaQiang < 0) {
						if (!this.beiDaQiangCount[key]) {
							this.beiDaQiangCount[key] = 0;
						}
						this.beiDaQiangCount[key] += -1;
					} else if (curDaQiang >= 1) {
						if (isCanQuanLeiDa && (this.jifen == 1 && curDaQiang == 4) || this.jifen != 1 && curDaQiang == 2) {
							if (!this.quanLeiDaCount[key]) {
								this.quanLeiDaCount[key] = 0;
							}
							this.quanLeiDaCount[key] += 1;
							isCanQuanLeiDa = false;
						} else {
							if (!this.daQiangCount[key]) {
								this.daQiangCount[key] = 0;
							}
							this.daQiangCount[key] += 1;
						}
					}
				}
			}
		}

		for (i = 0; i < memberCount; ++i) {

			//当前计算分数玩家
			let indexI = playingChairArr[i];
			guaiScore = 0;
			for (j = 0; j < memberCount; ++j) {

				//被计算分数的玩家
				let indexJ = playingChairArr[j];
				if (indexI != indexJ) {
					//马牌对马牌
					let user1Rate = 1;
					let user2Rate = 1;
					let maPaiRate = resout.rateArr[indexI] * resout.rateArr[indexJ];
					if (this.maRateType == 1 && maPaiRate > 2) {
						//单码牌 最高只有2
						maPaiRate = 2;
					}
					totalRate = user1Rate * user2Rate * maPaiRate;
					//打枪(全垒打)
					daqiang = resout.daqiangArr[indexI][indexJ];
					//统计打抢和被打枪的次数
					if (this.jifen == 1) {
						//加倍
						daqiang = Math.abs(resout.daqiangArr[indexI][indexJ]);
					}
					//this.showLog(`countResult begin daqiang = ${ daqiang } user1Rate = ${ user1Rate } user2Rate = ${ user2Rate } maPaiRate = ${ maPaiRate } maRateType = ${ this.maRateType } totalRate = ${ totalRate }`);
					for (k = 0; k < 3; ++k) {
						//计算打枪倍数
						let baseScore = resout.scoresArr[indexI][indexJ][k];
						endScore[indexI][k] += baseScore * totalRate;
						//默认翻倍
						let curScore = baseScore * (daqiang == 0 ? 1 : daqiang) * totalRate;
						if (this.jifen != 1) {
							//积分加1
							curScore = (baseScore + daqiang) * totalRate;
						}
						scoreArr[indexI] += curScore;
						//	this.showLog(`userScoresID = ${ indexI } user2 = ${ indexJ } totalRate = ${ totalRate }, baseScore = ${ baseScore }  daqiang = ${ daqiang } curScore = ${ curScore } score = ${ scoreArr[indexI]}`);
					}
					//this.showLog(`userScoresID = ${indexI} user2 = ${indexJ} score = ${scoreArr[indexI]}`);
					if (resout.guaipaiScoreArr[indexI] !== 0 && resout.guaipaiScoreArr[indexJ] === 0) {
						guaiScore += resout.guaipaiScoreArr[indexI] * totalRate * this.baseScore;
					} else if (resout.guaipaiScoreArr[indexI] === 0 && resout.guaipaiScoreArr[indexJ] !== 0) {
						guaiScore -= resout.guaipaiScoreArr[indexJ] * totalRate * this.baseScore;
					} else if (resout.guaipaiScoreArr[indexI] !== 0 && resout.guaipaiScoreArr[indexJ] !== 0) {
						if (resout.guaipaiScoreArr[indexI] > resout.guaipaiScoreArr[indexJ]) {
							guaiScore += resout.guaipaiScoreArr[indexI] * totalRate * this.baseScore;
						} else if (resout.guaipaiScoreArr[indexI] < resout.guaipaiScoreArr[indexJ]) {
							guaiScore -= resout.guaipaiScoreArr[indexJ] * totalRate * this.baseScore;;
						}
					}
				}
			}
			scoreArr[indexI] += guaiScore;
		}
		return { endScore, scoreArr };
	};


	/*----------------------------------------------------其他操作--------------------------------------------------------*/

	//清除定时器
	clearOperateTimer() {
		if (this.operateTimer) {
			clearTimeout(this.operateTimer);
			this.operateTimer = null;
		}
	}

	//用户默认操作
	userAutoOperate(uid) {

		if (this.roomDismissed) return;
		let user = this.getUserByUID(uid);
		if (user && user.playStatus == roomProto.userPlayEnum.PLAYING) {
			if (this.gameStatus == gameProto.gameStatus.NONE) {
				//等待状态默认准备操作
				this.userReady(user.userInfo.uid);

			} else if (this.gameStatus == gameProto.gameStatus.ROB_BANK) {
				//机器人默认不抢庄
				this.showLog(`[${uid}] default rob bank `, false);
				let robScale = this.userRobScoreList[0];
				if (user.userInfo.isRobot) {
					let robScaleIndex = utils.getRandomNum(0, this.userRobScoreList.length - 2);
					robScale = this.userRobScoreList[robScaleIndex];
				}
				this.onRobBank(uid, {
					robScale
				});

			} else if (uid != this.bankerUid && this.gameStatus == gameProto.gameStatus.POUR_SCORE) {
				//默认1倍
				this.showLog(`[${uid}] default user pour score`, false);
				let score = this.userBetScoreList[0];
				if (user.userInfo.isRobot) {
					let betIndex = utils.getRandomNum(0, this.userBetScoreList.length - 2);
					score = this.userBetScoreList[betIndex];
				}
				this.onPourScore(uid, {
					score
				});

			} else if (this.gameStatus == gameProto.gameStatus.SHOW_CARD) {
				//定牌时间延迟
				let rndTime = 0;
				if (user.userInfo.isRobot) {
					rndTime = utils.getRandomNum(5, 10);
				}

				setTimeout(() => {
					if (this.roomDismissed) return;
					let userCards = this.handCards[uid];
					let baiCards = userCards.slice();
					let info = this.showCardsUser.find(info => info.uid == uid)
					if (info.isShow) {
						return;
					}
					this.showLog(`[${uid}] default show card userCards = ${JSON.stringify(baiCards)} `);
					if (baiCards.length == 13 && this.gameLogic.getTeShuPaiCardType(baiCards)) {
						//有特殊牌型
						this.onShowCards(uid, { type: 1 });
					} else {
						//替换牌
						for (let i = 0; i < this.baoCards[uid].length; i++) {
							let detail = this.baoCards[uid][i];
							if (!detail.isEnd) {
								let baoCard = detail.baoCard;
								let endCard = detail.changeCard == -1 ? this.gameLogic.getChangeCard() : detail.changeCard;
								detail.endCard = endCard;
								detail.isEnd = true;
								this.handCards[uid].push(endCard);
								this.showLog(`[${uid}] default change card baoCard = ${baoCard} endCard = ${endCard} `);
							}
						}
						baiCards = this.handCards[uid].slice();
						let cardArr = this.gameLogic.autoSortCards(baiCards);
						this.onShowCards(uid, { type: 0, cards: cardArr });
					}
				}, rndTime * 1000);

			}
		}
	};

	//机器人自动操作
	robotAuto() {
		let userList = this.getPlayingUserList();
		for (let i = 0; i < userList.length; i++) {
			let curUid = userList[i];
			let user = this.getUserByUID(curUid);
			if (user && user.userInfo.isRobot) {
				let rndTime = utils.getRandomNum(1, 3);
				setTimeout(() => {
					this.userAutoOperate(curUid);
				}, rndTime * 1000);
			}
		}
	}

	//牌面回顾
	onGameReview(uid) {
		this.sendGameData(gameProto.gameReviewPush(this.reviewRecord), [uid]);
	};

	/**获取动画延迟时间 */
	getAniDelayTime() {
		let dt = 2
		let bipaiDt = 0.5 //每道牌比牌间隔
		let teshupaiDt = 1 //特殊牌间隔
		let daqiangDt = 1.3 //每次打枪间隔
		let quanleidaDt = 1.5 //全垒打间隔
		/**计算打枪和全垒打数量 */
		let dqCount = 0
		let quanleidaCount = 0
		let daqiangArr = this.result.daqiangArr
		for (const uid in daqiangArr) {
			const obj = daqiangArr[uid];
			for (const key in obj) {
				const score = obj[key];
				if (score == 4 && this.jifen == 1 || score == 2 && this.jifen == 2) {
					quanleidaCount = 1
				}
				if (score > 0) {
					dqCount++
				}
			}
		}

		if (this.biPaiType == 1) { //完整比牌动画
			dt += this.result.touSort.length * bipaiDt * 3 + this.result.teShuSort.length * teshupaiDt
			dt += dqCount * daqiangDt + quanleidaCount * quanleidaDt
		} else if (this.biPaiType == 2) { //简洁比牌动画
			dt += 3 * bipaiDt + dqCount * daqiangDt + quanleidaCount * quanleidaDt
			if (this.result.teShuSort.length > 0) {
				dt += bipaiDt
			}
		} else if (this.biPaiType == 3) { //跳过比牌动画
			dt = 0.5
		}
		return dt
	};

	//游戏结束
	async gameEnd() {

		//游戏未解散 开始下一局
		if (!this.roomDismissed) {

			this.resetPlayerStatus();
			let dt = this.getAniDelayTime();
			await this.delay(dt * 1000);

			await this.concludeGame();
			this.gameStatus = gameProto.gameStatus.NONE;
			this.sendGameStatus(this.gameStatus);
			this.showLog(`[ssssss] game ended reay to next`);

			//金币场自动准备
			this.totalTick = gameProto.gameStatusTm.RESULT;
			if (this.goldRoom && !this.roomDismissed && this.gameStatus == gameProto.gameStatus.NONE) {

				this.robotReadyTimer = setInterval(() => {

					if (this.gameStatus != gameProto.gameStatus.NONE) {
						if (this.robotReadyTimer) {
							clearInterval(this.robotReadyTimer);
							this.robotReadyTimer = null;
						}
					} else {
						let userList = this.getOnSeatUser();
						for (let i = 0; i < userList.length; i++) {
							let curUid = userList[i];
							let user = this.getUserByUID(curUid);
							if (user && user.userInfo.isRobot && user.playStatus == roomProto.userPlayEnum.NONE) {
								this.userReady(curUid);
							}
						}
					}
				}, 3000);
			}
		}
	}

	//房间解散 弹出总结算消息
	async onEventRoomDismiss(reason) {

		this.clearOperateTimer();
		let result = [];
		for (let key in this.userWinRecord) {
			let data = this.userWinRecord[key];
			result.push({
				uid: data.uid,
				nickname: data.nickname,
				avatar: data.avatar,
				score: data.score,
				winCount: this.winCount[key] || 0,
				loseCount: this.loseCount[key] || 0,
				tieCount: this.tieCount[key] || 0,
				daqiangCount: this.daQiangCount[key] || 0,
				beiDaQiangCount: this.beiDaQiangCount[key] || 0,
				tongHuaShunCount: this.tongHuaShunCount[key] || 0,
				teShuCount: this.teShuCount[key] || 0,
				quanLeiDaCount: this.quanLeiDaCount[key] || 0,
			});
		}

		let winMost = null;
		let loseMost = null;
		if (result.length > 0) {
			winMost = 0;
			loseMost = 0;
			for (let i = 1; i < result.length; ++i) {
				if (result[i].score > result[winMost].score) {
					winMost = i;
				}
				if (result[i].score < result[loseMost].score) {
					loseMost = i;
				}
			}
			winMost = result[winMost].uid;
			loseMost = result[loseMost].uid;
		}

		let creatorInfo = null;
		let createrUid = this.creatorInfo.ownerUid || this.creatorInfo.uid;
		let creator = await this.getOwnerInfo(createrUid);
		if (creator) {
			creatorInfo = {
				uid: creator.userInfo.uid,
				nickname: creator.userInfo.nickname,
				avatar: creator.userInfo.avatar,
			};
		}
		let plusInfo = { payDiamondUser: this.payDiamondUser };
		let message = gameProto.gameEndPush(result, winMost, loseMost, creatorInfo, plusInfo, reason);
		this.sendGameData(message);

	};


	/*================================测试功能========================================*/

	//获取下家位置
	getNextUserID(userId) {
		let chairID = this.userArr[userId].chairID;
		for (let i = 1; i <= this.chairCount; ++i) {
			let nextChairID = (chairID + i) % this.chairCount;
			let user = this.getUserByChairID(nextChairID);
			if (user && nextChairID < this.chairCount && user.playStatus == roomProto.userPlayEnum.PLAYING) {
				return user.userInfo.uid;
			}
		}
		return null;
	}

	//玩家换牌
	onChangeCard(uid, cardData) {

		let user = this.getUserByUID(uid);
		if (user.userInfo.isTest && cardData.length <= 13 && user.chairID < this.chairCount) {
			if (this.gameStatus != gameProto.gameStatus.SEND_CARDS) {
				//获取牌库
				let totalCards = this.gameLogic.getAllCards();
				let allCardData = this.gameLogic.getExceptCards(this.userTestCards, totalCards);
				if (this.userTestCards[uid]) {
					allCardData = allCardData.concat(this.userTestCards[uid]);
				}
				let isSuccess = this.isInCardList(cardData.slice(), allCardData.slice());
				this.showLog(`[${uid}] onChangeCard cardData = ${JSON.stringify(cardData)} isSuccess = ${isSuccess} `);
				if (isSuccess) {
					this.userTestCards[uid] = cardData;
				}
				this.sendGameData(gameProto.gameSetTestCardsPush(isSuccess), [uid]);
			} else {
				this.sendGameData(gameProto.gameSetTestCardsPush(false), [uid]);
			}
		}
	}

	//查询牌库
	onSearchCardsList(uid) {
		let user = this.getUserByUID(uid);
		if (user.userInfo.isTest && user.chairID < this.chairCount) {
			//获取牌库
			let totalCards = this.gameLogic.getAllCards();
			let allCardData = this.gameLogic.getExceptCards(this.userTestCards, totalCards);
			allCardData = allCardData.filter(item => { return item < this.baoCardsBegin });
			this.showLog(`[${uid}] onSearchCardsList userTestCards = ${JSON.stringify(this.userTestCards)} `);
			this.sendGameData(gameProto.gameGetTestCardsPush(uid, allCardData, this.userTestCards[user.uid]), [uid]);
		}
	}


	//-----------------------------暂时不要的 ---------------------------------------------//

	/*----------------------------------------------------抢庄--------------------------------------------------------*/

	//开始抢庄
	startRobBank() {

		this.clearOperateTimer();
		//开始抢庄
		this.gameStatus = gameProto.gameStatus.ROB_BANK;
		this.totalTick = gameProto.gameStatusTm.ROB_BANK;
		this.sendGameStatus(this.gameStatus, this.totalTick);
		this.endOperateTime = moment().add(this.totalTick, 's');
		this.robotAuto();
		this.operateTimer = setTimeout(() => {
			//定庄阶段 自动定庄
			if (this.gameStatus == gameProto.gameStatus.ROB_BANK) {
				let userList = this.getPlayingUserList();
				for (let i = 0; i < userList.length; i++) {
					let uid = userList[i];
					//用户是否下注
					if (!this.robBankList.find(item => item.uid == uid)) {
						this.userAutoOperate(uid);
					}
				}
			}
		}, this.totalTick * 1000);
	}

	//玩家抢庄
	onRobBank(uid, data) {

		if (this.gameStatus != gameProto.gameStatus.ROB_BANK) {
			this.showLog(`[${uid}]user robBank status error`, true);
			return;
		}

		if (this.robBankList.find(item => item.uid == uid)) {
			this.showLog(`[${uid}]user already rob bank`, true);
			return;
		}

		this.showLog(`[${uid}]user robBank data = ${JSON.stringify(data)} `, false);

		let curUser = this.getUserByUID(uid);
		let chairID = curUser.chairID;
		let robScale = data.robScale; //默认不抢

		if (!this.userRobScoreList.includes(robScale)) {
			this.showLog(`[${uid}]user rob bank robScale = ${robScale} list = ${JSON.stringify(this.userRobScoreList)}`, true);
			return;
		}

		if (chairID > this.chairCount) return;
		if (curUser.playStatus != roomProto.userPlayEnum.PLAYING) {
			this.showLog(`[${uid}]onRobBank fail playStatus != roomProto.userPlayEnum.PLAYING`, true);
			return;
		}
		let result = {
			uid,
			robScale
		};
		this.robBankList.push(result);
		this.sendGameData(gameProto.gameRobBankPush(uid, chairID, robScale));
		let playUser = this.getPlayingUserList();
		if (playUser.length == this.robBankList.length) {
			//定庄结束
			this.endRobBank();
		};
	}

	//抢庄结束
	endRobBank() {

		this.clearOperateTimer();
		let maxScale = 0;
		//抢庄的玩家
		for (let i = 0; i < this.robBankList.length; i++) {
			if (this.robBankList[i].robScale && this.robBankList[i].robScale > maxScale) {
				maxScale = this.robBankList[i].robScale;
			}
		}

		let robUser = this.robBankList.filter(item => item.robScale == maxScale);
		//无人抢庄
		if (robUser.length == 0 || maxScale == 0) {
			maxScale = 1;
			robUser = []
			let playingUserList = this.getPlayingUserList();
			playingUserList.forEach(uid => {
				robUser.push({
					uid,
					robScale: maxScale
				})
			});
			this.robBankList = robUser
		}
		this.bankerUid = robUser[Math.floor(Math.random() * robUser.length)].uid;
		//抢庄时 闲家倍数为抢庄倍数
		this.userBetScore[this.bankerUid] = { uid: this.bankerUid, score: maxScale };
		this.showLog(`set baneker bankerUid = ${this.bankerUid} maxScale = ${maxScale} `, false);
		//定庄通知
		this.sendGameData(gameProto.gameBankerPush(this.bankerUid, robUser));

		//默认动画两秒
		this.robDelayTime = 2000;
		this.startPourScore();
	}

	/*----------------------------------------------------用户下倍数--------------------------------------------------------*/

	//开始下分
	async startPourScore() {

		this.showLog(`game start pour score`, false);
		this.clearOperateTimer();
		this.gameStatus = gameProto.gameStatus.POUR_SCORE;
		this.totalTick = gameProto.gameStatusTm.POUR_SCORE;

		//抢庄动画延迟
		await this.delay(this.robDelayTime);

		this.sendGameStatus(this.gameStatus, this.totalTick);
		this.endOperateTime = moment().add(this.totalTick, 's');
		//推送玩家下注
		this.sendGameData(gameProto.gameUserPourScorePush(this.bankerUid, this.totalTick, this.userBetScoreList));
		this.robotAuto();
		this.operateTimer = setTimeout(() => {

			//默认操作 下最低分
			if (this.gameStatus == gameProto.gameStatus.POUR_SCORE) {
				let userList = this.getPlayingUserList();
				for (let i = 0; i < userList.length; i++) {
					let uid = userList[i];
					//用户是否压分
					if (!this.userBetScore[uid] && uid != this.bankerUid) {
						this.userAutoOperate(uid);
					}
				}
			}

		}, this.totalTick * 1000);
	}

	//玩家下分
	onPourScore(uid, data) {

		//庄家不能下注
		if (this.bankerUid == uid) return;

		if (this.gameStatus != gameProto.gameStatus.POUR_SCORE) {
			this.showLog(`[${uid}]user pourScore status error`, true);
			return;
		}

		//保存玩家投注记录
		if (this.userBetScore[uid]) {
			this.showLog(`[${uid}]user pourScore already error`, true);
			return;
		}

		let curUser = this.getUserByUID(uid);
		if (!curUser || curUser && curUser.playStatus != roomProto.userPlayEnum.PLAYING) {
			this.showLog(`[${uid}]onPourScore fail playStatus != roomProto.userPlayEnum.PLAYING`, true);
			return;
		}
		let score = data.score;
		if (!this.userBetScoreList.includes(score)) {
			this.showLog(`[${uid}]user rob bank score = ${score} list = ${JSON.stringify(this.userBetScoreList)}`, true);
			return;
		}

		this.userBetScore[uid] = {
			uid,
			score
		};
		//给玩家返回数据
		this.sendGameData(gameProto.gameUserPourScore(uid, score));
		let playUser = this.getPlayingUserList();

		this.showLog(`[${uid}] user pour score data = ${JSON.stringify(data)} playUser = ${playUser.length} betUser = ${utils.getLength(this.userBetScore)} `, false);

		//投注完毕 开始发牌
		if (playUser.length == utils.getLength(this.userBetScore)) {
			if (this.gameStatus == gameProto.gameStatus.POUR_SCORE) {
				this.endPourScore();
			}
		}
	}

	//结束下分
	endPourScore() {

		this.showLog(`[ssssss] game endPourScore`, false);
		//发最后的五张牌
		this.startSendCountCards(5);
	}



}

module.exports = gameFrame;